package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.businessdata.pojo.dto.BusinessContactListRelyDTO;
import com.xbongbong.pro.businessdata.pojo.vo.BusinessContactListRelyVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.BusinessContactService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 刘阳
 * @date 2019/11/19 3:34 PM
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/business/contact")
public class MobileBusinessContactController {

    @Resource
    private BusinessContactService businessContactService;

    /**
     * Description: 选择业务关联联系方式列表
     * @param businessContactListRelyDTO
     * @return String
     * @throws
     * @author 刘阳
     * @date 2019/11/11 3:56 PM
     * @since v1.0
     */
    @RequestMapping(value = "/contactList/getRely", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String businessProductListGetRely(@RequestBody @Valid BusinessContactListRelyDTO businessContactListRelyDTO, BindingResult br) {
        XbbResponse<BusinessContactListRelyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BusinessContactListRelyVO businessContactListGetRely = businessContactService.businessContactListGetRely(businessContactListRelyDTO);
                response = new XbbResponse<>(businessContactListGetRely);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
